package com.udf.AopShow.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by zwr on 2014/11/1.
 */
@Aspect
@Component
public class HelloAspect {

    @Pointcut("execution(* com.udf.AopShow.service..*.sayHi(..))")
    public void HiPT(){};

    @Pointcut("execution(* com.udf.AopShow.service..*.sayHello())")
    public void HelloPT(){};

    @Around(value = "HelloPT()")
    public void sayWelcome(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("----------------------------------");
        System.out.println("aop拦截方法:"+jp.getSignature().getName());
        System.out.println("AOP拦截目标："+jp.getThis().getClass().getName());
        System.out.println("aop hashcode：this-"+jp.getThis().hashCode()+" target-"+jp.getTarget().hashCode());
        MethodSignature m = (MethodSignature) jp.getSignature();
        System.out.println("declaringType:"+m.getDeclaringType());

        Method permissonMethod = m.getDeclaringType().getMethod(m.getMethod().getName(),String.class);
        System.out.println(permissonMethod.getName()+" "+permissonMethod.getParameterTypes());
        permissonMethod.invoke(jp.getTarget(), "aa");
    }

}
